package com.xingkeduo.service.cms.marketing;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import com.xingkeduo.model.param.ActivityParam.PreferentialModeEnum;
import com.xingkeduo.model.param.MarketingParam;
import com.xingkeduo.model.param.MarketingParam.MarketingServiceTypeEnum;
import com.xingkeduo.model.po.TMarketingWithBLOBs;
import com.xingkeduo.util.DateUtils;
import com.xingkeduo.util.StringUtils;
import com.xingkeduo.util.tool.ResponseMessage;

public interface MarketingTemplateService {
	/**
	 * 
	* @Title: saveOrUpdateCouponByTemplate 
	* @Description: 新增，更新券，卡模版 
	* @param @param couponTemplate
	* @param @return    设定文件 
	* @return ResponseMessage    返回类型 
	* @throws 
	* @author lixin
	* @date May 2, 2019 5:41:51 PM
	 */
	ResponseMessage saveOrUpdateCouponByTemplate(MarketingParam couponTemplate);
	/**O
	 * 
	* @Title: queryCouponById 
	* @Description: 查询券，卡模版详情 
	* @param @param couponTemplateId
	* @param @return    设定文件 
	* @return ResponseMessage    返回类型 
	* @throws 
	* @author lixin
	* @date May 2, 2019 5:42:16 PM
	 */
	ResponseMessage queryCouponById(Long couponTemplateId);
	/**
	 * 
	* @Title: queryCouponByTemplate 
	* @Description: 查询券，卡模版列表 
	* @param @param couponTemplateExample
	* @param @return    设定文件 
	* @return ResponseMessage    返回类型 
	* @throws 
	* @author lixin
	* @date May 2, 2019 5:42:40 PM
	 */
	ResponseMessage queryCouponByTemplate(MarketingParam couponTemplateExample);
	/**
	 * 
	* @Title: updateCouponStatusById 
	* @Description: 模版上下架 
	* @param @param couponTemplate
	* @param @return    设定文件 
	* @return ResponseMessage    返回类型 
	* @throws 
	* @author lixin
	* @date May 2, 2019 5:43:49 PM
	 */
	ResponseMessage updateCouponStatusById(MarketingParam couponTemplate);
	/**
	 * 
	* @Title: getALlCouponTemplateList 
	* @Description: 获取所有到券卡列表，只需要下发名字和id
	* @param @param couponTemplateExample
	* @param @return    设定文件 
	* @return ResponseMessage    返回类型 
	* @throws 
	* @author lixin
	* @date May 24, 2019 2:28:42 PM
	 */
	ResponseMessage getAllCouponTemplateList(MarketingParam couponTemplateExample);
	/**
	 * 
	* @Title: addTMarketingRelevant 
	* @Description: 添加券依赖
	* @param @param coupon    设定文件 
	* @return void    返回类型 
	* @throws 
	* @author lixin
	* @date May 29, 2019 5:16:24 PM
	 */
	void addTMarketingRelevant(MarketingParam coupon);
	/**
	 * 
	* @Title: updateTMarketingRelevant 
	* @Description: 更新券包-券卡依赖
	* @param @param oldTMarketingWith
	* @param @param couponTemplate    设定文件 
	* @return void    返回类型 
	* @throws 
	* @author lixin
	* @date Jun 8, 2019 8:33:46 PM
	 */
	void updateTMarketingRelevant(TMarketingWithBLOBs oldTMarketingWith, MarketingParam couponTemplate);
	
	public default void buildMarketing(MarketingParam couponTemplate,TMarketingWithBLOBs coupon) {
		BeanUtils.copyProperties(couponTemplate, coupon);
		if (StringUtils.isNotBlank(couponTemplate.getServiceType())
				&& coupon.getServiceType().equals(MarketingServiceTypeEnum.DISCOUNT.getType())) {
			coupon.setPreferentialMode(PreferentialModeEnum.DISCOUNT.getCode());
		} else if (StringUtils.isNotBlank(couponTemplate.getServiceType())
				&& coupon.getServiceType().equals(MarketingServiceTypeEnum.CASH.getType())) {
			coupon.setPreferentialMode(PreferentialModeEnum.REDUCE.getCode());
		} else if (StringUtils.isNotBlank(couponTemplate.getServiceType())
				&& coupon.getServiceType().equals(MarketingServiceTypeEnum.FREE.getType())) {
			coupon.setPreferentialMode(PreferentialModeEnum.FREE.getCode());
			coupon.setCouponAmount(BigDecimal.valueOf(0));
		} else if (StringUtils.isNotBlank(couponTemplate.getServiceType())
				&& coupon.getServiceType().equals(MarketingServiceTypeEnum.FREQUENCY.getType())) {
			coupon.setPreferentialMode(PreferentialModeEnum.FREE.getCode());
			coupon.setCouponAmount(BigDecimal.valueOf(0));
		} else if (StringUtils.isNotBlank(couponTemplate.getServiceType())) {
			coupon.setPreferentialMode(PreferentialModeEnum.SPECIAL_OFFER.getCode());
		}
		if (couponTemplate.getId() == null && couponTemplate.getReceiveStartTime() == null) {
			coupon.setReceiveStartTime(new Date());
		}
		if (couponTemplate.getId() == null && couponTemplate.getReceiveEndTime() == null) {
			coupon.setReceiveEndTime(DateUtils.addSomeMonths(coupon.getReceiveStartTime(), 36));
		}
		if (couponTemplate.getId() == null && couponTemplate.getUseStartTime() == null) {
			coupon.setUseStartTime(coupon.getReceiveStartTime());
		}
		if (couponTemplate.getUseType() != null
				&& couponTemplate.getUseType().equals(1)) {// 固定时间领取
			try {
				coupon.setUsePeriod(DateUtils.daysBetween(coupon.getUseStartTime(), coupon.getUseEndTime()));
			} catch (ParseException e) {
				coupon.setUsePeriod(0);
			}
			String useEndTimeStr = DateUtils.formatDate(DateUtils.getCurdate(coupon.getUseEndTime()),
					DateUtils.DATE_FORMAT_EN) + "235959";
			coupon.setUseEndTime(DateUtils.parseDate(useEndTimeStr));
		}else if(couponTemplate.getUseType() != null
				&& couponTemplate.getUseType().equals(0)) {//相对时间
			coupon.setUseEndTime(null);
		}
		String receiveEndTimeStr = DateUtils.formatDate(DateUtils.getCurdate(coupon.getReceiveEndTime()),
				DateUtils.DATE_FORMAT_EN) + "235959";
		coupon.setReceiveEndTime(DateUtils.parseDate(receiveEndTimeStr));

		coupon.setCreateBy(couponTemplate.getCreateBy());
		coupon.setUpdateBy(couponTemplate.getUpdateBy());
		if (couponTemplate.getId() != null) {
			coupon.setCreateBy(null);
		}
		if (CollectionUtils.isEmpty(couponTemplate.getActivityDaysParams())) {
			coupon.setWeekHourIds("0");
		}
		coupon.setUpdateTime(new Date());
	}
}
